package com.hantanger.weblog.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.hantanger.weblog.model.Category;
import com.hantanger.weblog.model.Comment;
import com.hantanger.weblog.util.HtmlUtil;
import com.hantanger.weblog.util.UserHelper;


public class JsonController extends BaseController {

	@Override
	public String doGet(HttpServletRequest request, HttpServletResponse response) {
		String method = request.getParameter("method");
		Object data = null;
		if("addClassName".equalsIgnoreCase(method)){
			data = addClassName(request);
		}else if("postcomment".equalsIgnoreCase(method)){
			data = postComment(request);
		}
		renderJson(data, response, JSON);
		return null;
	}
	
	/**
	 * 新增日志分类
	 * @param request
	 * @return
	 */
	private Object addClassName(HttpServletRequest request){
		String className = HtmlUtil.getString(request, "className", null);
		className = className.replaceAll(Category.SEP, "");
		long parentClassId = HtmlUtil.getLong(request, "parentClassId", 0);
		
		return getPostService().addCategory(className, parentClassId);
	}
	
	/**
	 * 日志评论
	 * @param request
	 * @return
	 */
	private Object postComment(HttpServletRequest request){
		long postId = HtmlUtil.getLong(request, "postId", 0);
		String nickname = HtmlUtil.getString(request, "nickname", null);
		String email = HtmlUtil.getString(request, "email", null);
		String url = HtmlUtil.getString(request, "url", null);
		String content = HtmlUtil.getString(request, "content", null);
		long parentId = HtmlUtil.getLong(request, "parentId", 0);
		if(parentId < 0){
			parentId = 0;
		}
		boolean isValid = true;
		if(StringUtils.isBlank(nickname) || StringUtils.isBlank(email) || StringUtils.isBlank(content)){
			isValid = false;
		}
		
		email = email.trim().toLowerCase();
		Matcher matcher = HtmlUtil.emailPattern.matcher(email);
		if(!matcher.matches()){
			isValid = false;
		}
		
		if(!isValid){
			return false;
		}
		
		long now = System.currentTimeMillis();
		if(parentId > 0){
			Comment parentComment = getPostService().getCommentById(parentId);
			if(parentComment == null){
				parentId = 0;
			}else{
				parentComment.setUpdateTime(now);
				getPostService().addComment(parentComment);
			}
		}
		
		Comment comment = new Comment();
		comment.setPostId(postId);
		comment.setUserName(nickname.trim());
		if(StringUtils.isNotBlank(url)){
			comment.setLink(url.trim());
		}
		comment.setEmail(email);
		comment.setPostId(postId);
		comment.setPublishTime(now);
		comment.setUpdateTime(now);
		comment.setContent(content.trim());
		comment.setIp((String)request.getAttribute("visitorIP"));
		comment.setParentId(parentId);
		
		if(UserHelper.isAdmin()){
			comment.setHost(true);
		}
		
		getPostService().addComment(comment);
		
		comment.setIp("");
		Map<String,Object> data = new HashMap<String,Object>();
		data.put("comment", comment);
		return getStringMerger().mergeTemplate(data, "comment.ftl");
	}
	
	@Override
	protected void fillSideBarData(HttpServletRequest request){
	}

}
